package com.battle.service.mvc;

import lombok.extern.slf4j.Slf4j;
import nirvana.core.security.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * MvcConfiguration
 *
 * @author linkanyway
 * @version 1.0
 * @date 2022/04/08 20:06
 */
@Configuration
@Slf4j
public class MvcConfiguration extends DispatcherServlet implements WebMvcConfigurer {

    /**
     * authentication interceptor
     */
    final AuthInterceptor authInterceptor;

    /**
     * constructor
     *
     * @param authInterceptor
     */
    public MvcConfiguration(AuthInterceptor authInterceptor) {
        this.authInterceptor = authInterceptor;
    }

    /***
     * add auth interceptor
     * todo: replaced by properties
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        List<String> excludePath = new ArrayList<> ();
        excludePath.add ("/swagger-ui.html");
        excludePath.add ("/swagger-ui.html/**");
        excludePath.add ("/swagger-resources/**");
        excludePath.add ("/configuration/**");
        excludePath.add ("/v2/api-docs");
        excludePath.add ("/webjars/**");
        excludePath.add("/api/login/web/testIdLogin");
        excludePath.add("/api/login/web/accountLogin");
        excludePath.add("/api/login/web/getAuthCode");
        excludePath.add("/api/login/web/refresh");
        excludePath.add("/api/user/web/createUser");



        excludePath.add("/api/login/test/idLogin");
        registry.addInterceptor (authInterceptor).addPathPatterns ("/**").excludePathPatterns (excludePath);

    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        WebMvcConfigurer.super.extendMessageConverters (converters);
    }

    /**
     * add resource
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler ("swagger-ui.html").addResourceLocations ("classpath:/META-INF/resources/");
        registry.addResourceHandler ("/webjars/**").addResourceLocations ("classpath:/META-INF/resources/webjars/");
    }

    /**
     * add converter
     *
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {

    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer
                .addPathPrefix("/api", c -> c.isAnnotationPresent(Controller.class))
                .addPathPrefix("/api", c -> c.isAnnotationPresent(RestController.class));
    }

}
